package com.syas.service.services;

import com.syas.dao.interfaces.IAttendInfoDao;
import com.syas.dao.interfaces.IFmgerDao;
import com.syas.dao.interfaces.IFrmgerPfmceDao;
import com.syas.dao.interfaces.ITargetDao;
import com.syas.model.models.Fmger;
import com.syas.model.models.FrmgerPfmce;
import com.syas.model.models.Target;
import com.syas.service.interfaces.IFmgerCalService;
import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Created by d on 2015/12/5.
 */
@Service("FmgerCalService")
public class FmgerCalService implements IFmgerCalService {

    @Autowired
    @Qualifier("FmgerDao")
    protected IFmgerDao fmgerDao;

    @Autowired
    @Qualifier("FrmgerPfmceDao")
    protected IFrmgerPfmceDao frmgerPfmceDao;

    @Autowired
    @Qualifier("AttendInfoDao")
    protected IAttendInfoDao attendInfoDao;

    @Autowired
    @Qualifier("TargetDao")
    protected ITargetDao targetDao;
    /**
     * 业绩计算
     */
    @Override
    public void  doYeJiCal(){
        int fmagerTotalCount=fmgerDao.countAll();
        int pageSize=1000;//每次计算1000个人
        int pagecount=fmagerTotalCount%pageSize==0?fmagerTotalCount/pageSize:fmagerTotalCount/pageSize+1;
        for(int pageNo=1;pageNo<=pagecount;pageNo++){
            Criteria crit = fmgerDao.getCriteria();
            crit.setFirstResult(pageSize*(pageNo-1));
            crit.setMaxResults(pageSize);
            List<Fmger> list = crit.list();
            for(Fmger fmger : list){
                fmgerYeJiCal(fmger);
            }
        }
    }

    /**
     * 理财师业绩计算
     */
    public void  fmgerYeJiCal(Fmger fmger){
        String cert_id=fmger.getCertId();//身份证号
        String ezbo_uname=fmger.getEzboUname();//e租宝账号
        String city=fmger.getWorkPlace();//城市
        String citygrade=fmger.getCityType();//城市级别
        String department_level1=fmger.getDepartmentLevel1();//一级部门
        String department_level2=fmger.getDepartmentLevel2();//二级部门
        String department_level3=fmger.getDepartmentLevel3();//三级部门
        String department_level4=fmger.getDepartmentLevel4();//四级部门
        String department_level5=fmger.getDepartmentLevel5();//五级部门
        String department_level6=fmger.getDepartmentLevel6();//六级部门
        String station=fmger.getPostType();//岗位
        String grade=fmger.getLevel();//等级
        String directleadername=fmger.getLeaderName();//直接领导
        String directleader_ezbouname=fmger.getLeaderEzboUname();//直接领导e租宝账号
        boolean buildperiod=fmger.getIsBuilding()=="是"?true:false;//是否筹建期
        //计算是否保护期
        Timestamp joindate=fmger.getJoinDate();
        boolean protection=isCurrMonthDate(joindate);//是否保护期
        //计算标准考核业绩
        Target target=getTarget(citygrade,station, grade);
        double standperf=target.getMonthtarget();//岗位标准考核业绩
        double avgtargetperf=target.getAvetarget();//人均业绩目标
        double jixsalybase=target.getJixSalaryBase();//绩效工资基数
        //计算个人业绩
        double perperf=getPerperf(fmger.getEzboUname());//个人业绩
        if(isTeamLeader(station)){//计算团队领导绩效和提成
            int archstaffnum=getArchstaffnumOfFmger(station);//标准架构编制
            //计算实际团队人数
            List<Fmger> teamfmgers=teamOfFmger(fmger);
            int currJoinNum=0;
            int currLeaveNum=0;
            for(Fmger fer:teamfmgers){
                if(isCurrMonthDate(fer.getJoinDate())){
                    currJoinNum=currJoinNum+1;
                }
                if(isCurrMonthDate(fer.getLeaveDate())){
                    currLeaveNum=currLeaveNum+1;
                }
            }
            int monStartNum=teamfmgers.size()-currJoinNum;
            int monEndNum=teamfmgers.size()-currLeaveNum;
            double actualTeamNum=(monEndNum+monStartNum)*0.5;//实际团队编制
            //计算管理业绩
            double manageperf=0;//管理业绩
            for(Fmger fer:teamfmgers){
                manageperf=manageperf+getPerperf(fer.getEzboUname());
            }
            //计算时标准架构编制和实际团队编制谁大取谁计算
            double calTeamNum=actualTeamNum<=archstaffnum?archstaffnum:actualTeamNum;
            double targetperf=avgtargetperf*calTeamNum;//岗位实际考核目标
            //计算目标达成率
            double targetreachrate=(manageperf*1.0)/targetperf;//目标达成率
            //计算开单率
            int kandanNum=0;
            for(Fmger fer:teamfmgers){
                double perperftemp=getPerperf(fer.getEzboUname());
                if(perperftemp>=10000){
                    kandanNum=kandanNum+1;
                }
            }
            double kaidanrate=(kandanNum*1.0)/teamfmgers.size();//开单率
            //计算团队赎回率
            double totalInvestAmt=0;
            double redeInvestAmt=0;
            for(Fmger fer:teamfmgers){
                List<FrmgerPfmce> frmgerPfmces=frmgerPfmceDao.pfmceWaterOfFmger(fer.getEzboUname());
                for(FrmgerPfmce frmgerPfmce:frmgerPfmces){
                    if(isCurrMonthDate(frmgerPfmce.getRedetime())){
                        redeInvestAmt=redeInvestAmt+frmgerPfmce.getInvestamt();
                    }
                    totalInvestAmt=totalInvestAmt+frmgerPfmce.getInvestamt();
                }
            }
            double shuhuirate=(redeInvestAmt*1.0)/totalInvestAmt;//赎回率
        }else{//计算财富管理师绩效
            double targetreachrate=perperf*1.0/standperf;//目标达成率
            //绩效工资发放比例
            double jixsalyrate=targetreachrate<0.3?0:targetreachrate;
            double jixsaly=jixsalybase*jixsalyrate;//绩效工资
        }
    }

    /**
     * 判断是否当月日期(当月是指计算月的上个月)
     */
    public boolean isCurrMonthDate(Timestamp date){
        boolean isCurrMonthDate=false;
        if(date!=null){
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(new Date(date.getTime()));
            Calendar calendar2 = Calendar.getInstance();
            calendar2.add(Calendar.MONTH, -1);
            int yearDiffer=calendar2.get(Calendar.YEAR)-calendar1.get(Calendar.YEAR);
            int monthDiffer = calendar2.get(Calendar.MONTH)-calendar1.get(Calendar.MONTH);
            isCurrMonthDate=(yearDiffer==0&&monthDiffer==0)?true:false;
        }
        return isCurrMonthDate;
    }

    /**
     * 根据e租宝用户名获取个人业绩
     */
    public double getPerperf(String ezboUname){
        double perperf=0;
        List<FrmgerPfmce> frmgerPfmces=frmgerPfmceDao.pfmceWaterOfFmger(ezboUname);
        for(FrmgerPfmce frmgerPfmce:frmgerPfmces){
            perperf=perperf+frmgerPfmce.getAchieve();
        }
        return perperf;
    }

    /**
     * 是否团队领导
     */
    public boolean isTeamLeader(String station){
        return !("财富管理师".equals(station));
    }
    /**
     * 理财师的团队(包括当月入职的和当月离职的人员)
     */
    public List<Fmger> teamOfFmger(Fmger fmger){
        List<Fmger> fmgers=new ArrayList<Fmger>();
        String station=fmger.getPostType();//岗位
        if(station.equals("团队经理")){
            List<Fmger> treasures=fmgerDao.directSubordteOfFmger(fmger);
            for(Fmger treasure:treasures){
                fmgers.add(treasure);
            }
        }else if(station.equals("分部部长")){
            List<Fmger> teamLeaders=fmgerDao.directSubordteOfFmger(fmger);
            for(Fmger teamLeader:teamLeaders){
                fmgers.add(teamLeader);
                List<Fmger> treasures=fmgerDao.directSubordteOfFmger(teamLeader);
                for(Fmger treasure:treasures){
                    fmgers.add(treasure);
                }
            }
        }else if(station.equals("营业部经理")){
            List<Fmger> unitManagers=fmgerDao.directSubordteOfFmger(fmger);
            for(Fmger unitManager:unitManagers){
                fmgers.add(unitManager);
                List<Fmger> teamLeaders=fmgerDao.directSubordteOfFmger(unitManager);
                for(Fmger teamLeader:teamLeaders){
                    fmgers.add(teamLeader);
                    List<Fmger> treasures=fmgerDao.directSubordteOfFmger(teamLeader);
                    for(Fmger treasure:treasures){
                        fmgers.add(treasure);
                    }
                }
            }
        }else if(station.equals("分公司总经理")){
            List<Fmger> divisionManagers=fmgerDao.directSubordteOfFmger(fmger);
            for(Fmger divisionManager:divisionManagers){
                fmgers.add(divisionManager);
                List<Fmger> unitManagers=fmgerDao.directSubordteOfFmger(divisionManager);
                for(Fmger unitManager:unitManagers){
                    fmgers.add(unitManager);
                    List<Fmger> teamLeaders=fmgerDao.directSubordteOfFmger(unitManager);
                    for(Fmger teamLeader:teamLeaders){
                        fmgers.add(teamLeader);
                        List<Fmger> treasures=fmgerDao.directSubordteOfFmger(teamLeader);
                        for(Fmger treasure:treasures){
                            fmgers.add(treasure);
                        }
                    }
                }
            }
        }else if(station.equals("区域总监")){
            List<Fmger> cityManagers=fmgerDao.directSubordteOfFmger(fmger);
            for(Fmger cityManager:cityManagers){
                fmgers.add(cityManager);
                List<Fmger> divisionManagers=fmgerDao.directSubordteOfFmger(cityManager);
                for(Fmger divisionManager:divisionManagers){
                    fmgers.add(divisionManager);
                    List<Fmger> unitManagers=fmgerDao.directSubordteOfFmger(divisionManager);
                    for(Fmger unitManager:unitManagers){
                        fmgers.add(unitManager);
                        List<Fmger> teamLeaders=fmgerDao.directSubordteOfFmger(unitManager);
                        for(Fmger teamLeader:teamLeaders){
                            fmgers.add(teamLeader);
                            List<Fmger> treasures=fmgerDao.directSubordteOfFmger(teamLeader);
                            for(Fmger treasure:treasures){
                                fmgers.add(treasure);
                            }
                        }
                    }
                }
            }
        }
        return fmgers;
    }


    /**
     * 获取标准架构编制人数
     */
    public int getArchstaffnumOfFmger(String station){
        int archstaffnum=9;
        if(station.equals("团队经理")){
            archstaffnum=9;
        }else if(station.equals("分部部长")){
            archstaffnum=27;
        }else if(station.equals("营业部经理")){
            archstaffnum=45;
        }else if(station.equals("分公司总经理")){
            archstaffnum=135;
        }else if(station.equals("区域总监")){
            archstaffnum=135*fmgerDao.getBrachComyNum();
        }
        return archstaffnum;
    }
    /**
     * 获取理财师业绩目标
     */
    public Target getTarget(String citygrade,String station,String grade){
        String citygd="1";
        if(citygrade.equals("一类城市")){
            citygd="1";
        }else if(citygrade.equals("二类城市")){
            citygd="2";
        }else if(citygrade.equals("三类城市")){
            citygd="3";
        }else{
            citygd="4";
        }
        Target target=targetDao.getTargetByGrageCode(citygd,grade);
        if(station.equals("区域总监")){
            int brachComyNum=fmgerDao.getBrachComyNum();
            double monthtarget=target.getAvetarget()*135*brachComyNum;
            target.setMonthtarget(monthtarget);
        }
        return target;
    }
}
